package com.matrix.rabbitmqtest.direct;

import cn.hutool.core.lang.Console;
import com.matrix.rabbitmqtest.util.AppKeys;
import com.matrix.rabbitmqtest.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 测试fanout交换机
 * LogConsumer2
 */
public class InfoConsumer {

    public static void main(String[] args) throws IOException {
        // 1. 随便得到一个channel
        Channel channel = RabbitMQUtil.getChannel();
        // 2. 声明一个交换机
        channel.exchangeDeclare(AppKeys.DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT);
        // 3. 声明一个随机queue
        String queueName = channel.queueDeclare().getQueue();
        // 4. 绑定到交换机上
        channel.queueBind(queueName,AppKeys.DIRECT_EXCHANGE , "/info");
        // 5. 接收log
        Console.log("InfoConsumer等待接收消息...");
        channel.basicConsume(queueName, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Console.log("InfoConsumer Receiving {} : {}", envelope.getDeliveryTag(), new String(body, StandardCharsets.UTF_8));
            }
        });

    }
}
